package net.neimeng.luculent.commonmodule;

import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.igexin.sdk.PushManager;

import net.neimeng.luculent.commonmodule.databinding.CommonActivityLoginBinding;
import net.neimeng.luculent.commonmodule.entities.User;
import net.neimeng.luculent.commonmodule.router.CommonRouterPath;
import net.neimeng.luculent.commonmodule.viewmodel.LoginViewModel;
import net.neimeng.luculent.coremodel.base.lifecycle.LCObserver;
import net.neimeng.luculent.coremodel.base.view.BaseActivity;
import net.neimeng.luculent.coremodel.datamodel.http.MapResult;
import net.neimeng.luculent.coremodel.datamodel.http.Result;
import net.neimeng.luculent.coremodel.utils.LoadingDialog;
import net.neimeng.luculent.coremodel.utils.SPConstant;

import java.util.Locale;

@Route(path = CommonRouterPath.LOGIN)
public class LoginActivity extends BaseActivity {

    private CommonActivityLoginBinding binding;

    private LoginViewModel loginViewModel;

    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.common_activity_login);
        binding.setClick(this);

    }

    @Override
    protected void onInit() {
        loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        onObserve();
        changeIconSize();
    }

    //观察 viewModel中的数据
    private void onObserve(){
        loginViewModel.getCode().observe(this, new LCObserver<Result>() {
            @Override
            public void onSuccess(Result result) {
                ToastUtils.showShort("验证码已发送");
            }
        });

        loginViewModel.getLogin().observe(this, new LCObserver<MapResult<User>>() {
            @Override
            public void onSuccess(MapResult<User> result) {
                User user = result.getData();
                //保存当前登录用户的基本信息
                SPUtils spUtils = SPUtils.getInstance();
                spUtils.put(SPConstant.HTTP_TOKEN,user.getTOKEN());
                spUtils.put(SPConstant.USER_NAME,user.getNAME());
                spUtils.put(SPConstant.USER_PHOTO,user.getICON());
                spUtils.put(SPConstant.MENU_ROLE,user.getMENU_ROLE());
                ARouter.getInstance().build(CommonRouterPath.MAIN).navigation();
                finish();
            }
        });
    }

    //动态修改 EditText drawableLeft 图标的尺寸
    private void changeIconSize(){

        Drawable drawable = getResources().getDrawable(R.drawable.iconfont_phone);
        drawable.setBounds(0, 0, 70, 70);
        binding.username.setCompoundDrawables(drawable, null, null, null);

        drawable = getResources().getDrawable(R.drawable.iconfont_lock);
        drawable.setBounds(0, 0, 70, 70);
        binding.yanzhengma.setCompoundDrawables(drawable, null, null, null);
    }


    //发送验证码按钮的click事件
    public void getCode(View view){
        String cellphoneNo = binding.username.getText().toString().trim();
        if (cellphoneNo.length() != 11) {
            binding.username.setError("请输入合法手机号");
            return;
        }
        LoadingDialog.submitShow(this);
        startTiming();
        loginViewModel.getCode(cellphoneNo);
    }

    //开启倒计时
    public void startTiming() {
        if(countDownTimer == null){
            countDownTimer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    binding.sendYan.setEnabled(false);
                    binding.sendYan.setText(String.format(Locale.getDefault(),"%d秒 后发送", millisUntilFinished / 1000));
                }
                @Override
                public void onFinish() {
                    binding.sendYan.setEnabled(true);
                    binding.sendYan.setText("获取验证码");
                }
            };
        }
        countDownTimer.start();
    }

    //登录按钮的 click事件
    public void login(View view){
        String cellphoneNo = binding.username.getText().toString().trim();
        String code = binding.yanzhengma.getText().toString().trim();
        if (cellphoneNo.length() != 11) {
            binding.username.setError("请输入合法手机号");
            return;
        }
        if (code.length() != 6) {
            binding.yanzhengma.setError("请输入合法验证码");
            return;
        }
        LoadingDialog.submitShow(this);
        loginViewModel.login(cellphoneNo,code, PushManager.getInstance().getClientid(this));
    }

}
